package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.ContractExample;
import cn.itcast.domain.cargo.Export;
import cn.itcast.domain.cargo.ExportExample;
import cn.itcast.domain.cargo.ExportProduct;
import cn.itcast.domain.system.User;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExportProductService;
import cn.itcast.service.cargo.ExportService;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/export")
public class ExportContractller extends BaseController {
    @Reference
    private ContractService contractService;
    @Reference
    private ExportService exportService;
    @Reference
   private ExportProductService exportProductService;


    @RequestMapping("/contractList")
    public String contractList(@RequestParam(defaultValue="1") int page, @RequestParam(defaultValue="10")int pageSize){
        ContractExample contractExample = new ContractExample();
        contractExample.createCriteria().andCompanyIdEqualTo(getCompanyId()).andStateEqualTo(1);
       contractExample.setOrderByClause("create_time desc");
        PageInfo pageInfo = contractService.findAll(contractExample, page, pageSize);
        request.setAttribute("page",pageInfo);
        return "cargo/export/export-contractList";
    }
    @RequestMapping(value = "/list",name ="查询报运单数据")
    public String exportList(@RequestParam(defaultValue = "1",name = "page") int pageNum, @RequestParam(defaultValue = "10") int pageSize){
        ExportExample exportExample = new ExportExample();
        exportExample.createCriteria().andCompanyIdEqualTo(getCompanyId());
        exportExample.setOrderByClause("create_time desc");
        PageInfo pageInfo = exportService.findAll(exportExample, pageNum, pageSize);
        request.setAttribute("page",pageInfo);
        return "cargo/export/export-list";
    }
    @RequestMapping(value = "/toExport",name ="进入填写报运单页面")
    public String toExport(String id){
        request.setAttribute("id",id);
        return "cargo/export/export-toExport";
    }
    @RequestMapping(value = "/edit",name ="保存保运单方法")
    public String edit(Export export){
        User user = getUser();
        if (StringUtils.isEmpty(export.getId())){
            export.setId(UUID.randomUUID().toString());
            export.setCreateBy(user.getId());
            export.setCreateDept(user.getDeptId());
            export.setCreateTime(new Date());
            export.setCompanyId(getCompanyId());
            export.setCompanyName(getCompanyName());
            exportService.save(export);
        }else {
            export.setUpdateTime(new Date());
            export.setUpdateBy(user.getId());
            exportService.update(export);
        }
        return "redirect:cargo/export/list.do";
    }
    @RequestMapping(value="/toUpdate" ,name="进入修改报运单页面")
    public String toUpdate(String id){
        Export export = exportService.findById(id);
        request.setAttribute("export",export);
        List<ExportProduct> exportProductList = exportProductService.findByExportId(id);

        request.setAttribute("eps",exportProductList);
        return "cargo/export/export-update";
    }
}
